//
//  SNKShareManager.m
//  SleepNews
//
//  Created by Kavee DJ on 2016/12/16.
//  Copyright © 2016年 Kavee DJ. All rights reserved.
//

#import "SNKShareManager.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>

@implementation SNKShareManager
static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });
    return _instance;
}

+ (instancetype)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (void)shareWeiboWithTitle:(NSString *)title text:(NSString *)text images:(UIImage *)image url:(NSURL *)url dismissBlock:(void(^)())dismissBlock
{
    // 1.创建分享参数
    NSArray *imageArray = @[[UIImage imageNamed:@"xiaoying018.png"]];
    
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    
    [shareParams SSDKEnableUseClientShare];  // 使用客户端,分享
    
    [shareParams SSDKSetupShareParamsByText:text?text:@"终于有一款新闻APP能达到我灵魂的高度,这么有毒的APP还有谁没有入坑吗?我不信!"
                                     images:image?image:imageArray
                                        url:url?url:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/id1190231678"]
                                      title:title?title:@"这个APP,竟然这么棒!"
                                       type:SSDKContentTypeAuto];
    
    // 2.简单分享
//    [ShareSDK share:SSDKPlatformTypeSinaWeibo parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
//        switch (state) {
//            case SSDKResponseStateSuccess:
//            {
//                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
//                                                                    message:nil
//                                                                   delegate:nil
//                                                          cancelButtonTitle:@"OK"
//                                                          otherButtonTitles:nil];
//                [alertView show];
//                dismissBlock();
//            }
//                break;
//            case SSDKResponseStateFail:
//            {
//                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
//                                                                    message:nil
//                                                                   delegate:nil
//                                                          cancelButtonTitle:@"确定"
//                                                          otherButtonTitles:nil];
//                [alertView show];
//                dismissBlock();
//            }
//                break;
//                
//            default:
//                break;
//        }b
//    }];
    //显示分享菜单
    [ShareSDK showShareActionSheet:nil
                             items:nil
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                           message:[NSString stringWithFormat:@"%@",error]
                                                                          delegate:nil
                                                                 cancelButtonTitle:@"OK"
                                                                 otherButtonTitles:nil, nil];
                           [alert show];
                           break;
                       }
                       default:
                           break;
                   }
               }
     ];
}


@end
